package com.migo.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 * Created by Administrator on 2017/6/9 0009.
 */
public class JsonUtils {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public static String objectToJson(Object data){
        try{
            String str = MAPPER.writeValueAsString(data);
            return str;
        } catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T jsonToPojo(String jsonData,Class<T> beanType){
        try{
            T t = MAPPER.readValue(jsonData,beanType);
            return t;
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType){
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class,beanType);
        try{
            List<T> list = MAPPER.readValue(jsonData,javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
